/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BuildData;

/**
 *
 * @author Pham Thanh
 */
import DecisionTree.AttributeSet;
import DecisionTree.DecisionTree;
import DecisionTree.DecisionTreeBuilder;
import DecisionTree.ItemSet;
import DecisionTree.SymbolicAttribute;
import DecisionTree.io.ItemSetReader;
import DecisionTree.io.ReadWriteDecisionTree;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

public final class NgangCheoTree {

    private AttributeSet attributeSet;
    private DecisionTree tree;
    private ItemSet learningSet;

    public NgangCheoTree() throws IOException, FileNotFoundException, ClassNotFoundException {

        System.out.println("package BuildData; class NgangCheoTree; method DecisionTree buildTree");

        createDecisionTree();
    }

    public void createDecisionTree() throws IOException {
        this.learningSet = null;
        try {
            //learningSet = ItemSetReader.read(new FileReader("data\\dt\\tree.db"));
            learningSet = ItemSetReader.read("data\\tree\\ngangcheotreedata.db");
        } catch (IOException e) {
            System.out.println("khong doc duoc file");
        }

        this.attributeSet = learningSet.attributeSet();

        Vector testAttributesVector = new Vector();

        testAttributesVector.add(attributeSet.findByName("tknBf2"));
        testAttributesVector.add(attributeSet.findByName("tknBf1"));
        testAttributesVector.add(attributeSet.findByName("nswStr"));
        testAttributesVector.add(attributeSet.findByName("tknAf1"));
        testAttributesVector.add(attributeSet.findByName("tknAf2"));


        AttributeSet testAttributes = new AttributeSet(testAttributesVector);
        SymbolicAttribute goalAttribute =
                (SymbolicAttribute) learningSet.attributeSet().findByName("classNsw");

        this.tree = buildTree(learningSet, testAttributes,
                goalAttribute);
        ReadWriteDecisionTree rwd = new ReadWriteDecisionTree();
        rwd.writeToFile("data//tree//ngangcheotree.db", tree);

    }

    static private DecisionTree buildTree(ItemSet learningSet,
            AttributeSet testAttributes,
            SymbolicAttribute goalAttribute) {
        
        DecisionTreeBuilder builder = new DecisionTreeBuilder(learningSet, testAttributes, goalAttribute);
        return builder.build().decisionTree();
    }
}
